#coding=utf-8
# -*- coding: UTF-8 -*-
#!/usr/bin/python

import json


data1 = {
    'COM':"",
    'baudrate': "115200",
    'type' : "103",
    'otafile': "",
    'dsel':[1,1,1,1,1,1],
    'ddiv':[2,2,2,2,2,2],
    'bitcfg':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    'devcfg':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    'usercfg':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
}

default_file = "user_cfg.js"

class jscfg():
    def __init__(self):
        self.filename = default_file
        self.data = self.read()
        
    def read(self):
        filename = self.filename
        #print('conf.js is', filename, flush=True)
        try:
            with open(filename, 'r+') as f:
                data = json.load(f)
            return data    
        except IOError as err:
            print("write new");
            with open(filename, 'w+') as f:
                json.dump(data1, f,indent=4)
            return data1
        
    def save(self):
        filename = self.filename
        print('conf.js is', filename, flush=True)
        try:
            with open(filename, 'w+') as f:
                json.dump(self.data, f,indent=4)
                print("write ok");
        except IOError as err:
            print("write err");




